﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Data;
namespace FTchina.SMS
{
    /// <summary>
    ///CustomerInfo 的摘要说明
    /// </summary>
    public class CustomerInfo
    {
        public CustomerInfo()
        {
            //
            //TODO: 在此处添加构造函数逻辑
            //
        }


        /// <summary>
        /// 通过groupid获取group名称
        /// </summary>
        /// <param name="groupID"></param>
        /// <returns></returns>
        public static string GetCustomGroupByGroupID(string groupID)
        {
            try
            {
                string sql = "select groupname from CustomerGroup where id={0}";
                using (DataTable dt = shujuku.GetDataTable(string.Format(sql, groupID)))
                {
                    return dt.Rows[0][0].ToString();
                }
            }
            catch (Exception e)
            {
                return "";
            }
        }



        /// <summary>
        /// 检查来电号码是否在数据库中
        /// </summary>
        /// <param name="phone"></param>
        public static Boolean CheckCustomer(string phone)
        {
            string testsql = "select * from CustomerList  where CustomerTel0='{0}' or Customerlandline0='{0}'";

            return shujuku.testread(string.Format(testsql, phone));
        }


        /// <summary>
        /// 通过用户号码获取客户ID
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static String GetCustomerIDByPhone(string phone)
        {
            string sql = "select id from CustomerList  where CustomerTel0='{0}' or Customerlandline0='{0}'";
            //  LogNet.LogBLL.info(string.Format(testsql, phone));
            try
            {
                return shujuku.GetDataTable(string.Format(sql, phone)).Rows[0][0].ToString();
            }
            catch (Exception e)
            {
                //FTchina.basic.Log(string.Format(sql, phone)+e.Message);
                return "";
            }
        }


        /// <summary>
        /// 来电次数增加一
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static Boolean CallCountPlusOneBy(string phone)
        {
            string sql = "update CustomerList set callcount=callcount+1,LastCallDate='{0}' where CustomerTel0='{1}' or Customerlandline0='{1}'";
            return shujuku.ExecuteSql(string.Format(sql, basic.GetSqliteDate(), phone));

        }


        /// <summary>
        /// 通过用户id获取姓名
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static string GetCustomerNameByID(string CustomerID)
        {
            string _CustomerName = string.Empty;
            if (CustomerID.Trim().Length > 0)
            {
                if (CustomerID == "009")
                {
                    _CustomerName = "到店客户";
                }
                else
                {//电话客户
                    string sql = "select CustomerName from CustomerList where id={0}";
                    try
                    {
                        _CustomerName = shujuku.GetDataTable(string.Format(sql, CustomerID)).Rows[0][0].ToString();
                    }
                    catch (Exception e)
                    {

                    }
                }
            }
            return _CustomerName;
        }

        /// <summary>
        /// 通过用户最后一个CustomerID
        /// </summary>
        /// <param name="CustomerID"></param>
        /// <returns></returns>
        public static string GetLastCustomerID()
        {

            string sql = "select id from CustomerList order by id desc";

            return shujuku.GetDataTable(sql).Rows[0][0].ToString();

        }


        /// <summary>
        /// 通过客户分组Id获取组内客户的姓名
        /// </summary>
        /// <param name="GroupID"></param>
        /// <returns></returns>
        public static string GetCustomerNamesByGroupID(string GroupID)
        {
            string tmp = string.Empty;
            string sql = "select CustomerName from CustomerList where CustomerGroupID ='{0}'";
            using (DataTable dt = shujuku.GetDataTable(string.Format(sql, GroupID)))
            {
                foreach (DataRow dr in dt.Rows)
                {
                    tmp += dr["CustomerName"] + ",";
                }
            }
            if (tmp.Length == 0) tmp = "无对应客户名单";
            return tmp;
        }



        /// <summary>
        /// 获取发送成功的短信条数
        /// </summary>
        /// <returns></returns>
        public static int GetSendedSucessCount()
        {
            string sql = "select sum([count]) from send_log where result like '%成功%'";

            string count = string.Empty;
            try
            {
                count = shujuku.GetDataTable(sql).Rows[0][0].ToString();
                if (count.Length == 0) count = "0";
            }
            catch (Exception e)
            {
                count = "0";
            }
            return Convert.ToInt32(count);
        }
        /// <summary>
        /// 获取发送失败的短信条数
        /// </summary>
        /// <returns></returns>
        public static int GetSendedUnSucessCount()
        {
            string count = string.Empty;
            string sql = "select sum([count]) from send_log where result not like '%成功%'";
            try
            {
                count = shujuku.GetDataTable(sql).Rows[0][0].ToString();
                if (count.Length == 0) count = "0";
            }
            catch (Exception e)
            {
                count = "0";
            }

            return Convert.ToInt32(count);
        }






        /// <summary>
        /// 通过客户号码获取用户的相关信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static String GetCusetomerInfoByPhone(string phone)
        {
            string tmp = string.Empty;
            string format = "{0}，{1}，{2}";
            string id = FTchina.SMS.CustomerInfo.GetCustomerIDByPhone(phone);
            if (id.Length > 0)
            {
                FTchina.Models.CustomerList mCustomerList = FTchina.BLL.CustomerListManager.GetCustomerListByid(Convert.ToInt32(id));
                tmp = string.Format(format, mCustomerList.CustomerName, mCustomerList.CustomerTel0, mCustomerList.CustomerWork);

            }
            if (tmp.EndsWith("，")) tmp = tmp.Substring(0, tmp.Length - 1);
            return tmp;
        }

        /// <summary>
        /// 通过客户ID获取用户的相关信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static String GetCusetomerInfoByID(string CustomerID)
        {
            string tmp = string.Empty;
            string format = "{0}，{1}，{2}";
            string tmpchenghu = string.Empty;
            if (CustomerID != "009" && CustomerID.Length > 0)
            {
                FTchina.Models.CustomerList mCustomerList = FTchina.BLL.CustomerListManager.GetCustomerListByid(Convert.ToInt32(CustomerID));
                if (mCustomerList != null)
                {
                    if (mCustomerList.CustomerName.Length == 1)
                    {//名字只有一个字的时候加上称呼
                        tmpchenghu = (mCustomerList.CustormeSex == "男") ? "先生" : "女士";
                        tmpchenghu = mCustomerList.CustomerName + tmpchenghu;
                    }
                    else
                    {
                        tmpchenghu = mCustomerList.CustomerName;
                    }
                    tmp = string.Format(format, tmpchenghu, mCustomerList.CustomerTel0, mCustomerList.CustomerWork);
                }
            }
            if (CustomerID == "009" && CustomerID.Length > 0)
            {
                tmp = "到店客户";
            }
            if (tmp.EndsWith("，")) tmp = tmp.Substring(0, tmp.Length - 1);
            return tmp;
        }
        /// <summary>
        /// 通过客户ID获取用户的相关信息
        /// </summary>
        /// <param name="phone"></param>
        /// <returns></returns>
        public static String GetCusetomerChenhuByID(string CustomerID)
        {
            string tmp = string.Empty;
            string format = "{0}";
            string tmpchenghu = string.Empty;
            if (CustomerID != "009" && CustomerID.Length > 0)
            {
                FTchina.Models.CustomerList mCustomerList = FTchina.BLL.CustomerListManager.GetCustomerListByid(Convert.ToInt32(CustomerID));
                if (mCustomerList != null)
                {
                    if (mCustomerList.CustomerName.Length == 1)
                    {//名字只有一个字的时候加上称呼
                        tmpchenghu = (mCustomerList.CustormeSex == "男") ? "先生" : "女士";
                        tmpchenghu = mCustomerList.CustomerName + tmpchenghu;
                    }
                    else
                    {
                        tmpchenghu = mCustomerList.CustomerName;
                    }
                    tmp = string.Format(format, tmpchenghu);
                }
            }
            if (CustomerID == "009" && CustomerID.Length > 0)
            {
                tmp = "到店客户";
            }
            if (tmp.EndsWith("，")) tmp = tmp.Substring(0, tmp.Length - 1);
            return tmp;
        }
    }
}